﻿using System.Collections;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
public interface IResultSceneUI
{
    void OnClickAgain();
    void OnClickSkip();
    void OnClickAdMulti();
    void OnClickNext();
    void OnProgressAnimEnd();
    void OnClickGoBack();
}

[System.Serializable]
public class PersonalResultPanelData
{
    public bool win;
    public string playerName;
    public int coin;
    public int level;
    public int star;
    public int mapId;
    //public int equipCount;
}


public class ResultSceneUI : PanelCommon
{
    public IResultSceneUI uiListener;

    public Text addScoreText;
    public Text levelText;

    public GameObject winGo;
    public GameObject loseGo;

    public GameObject againBtn;
    public GameObject adMultiBtn;
    public GameObject nextBtn;
    
    public Transform starParent;

    public RuntimeAnimatorController loseAnimCtrl;

    public Star starPref;

    protected PersonalResultPanelData m_Data;

    // Start is called before the first frame update
    public override void Awake()
    {
        base.Awake();
        
        if(winGo)
            winGo.SetActive(true);
        if(loseGo)
            loseGo.SetActive(true);
        
        if(starPref)
            starPref.gameObject.SetActive(false);
    }


    public virtual void Init(PersonalResultPanelData data)
    {
        m_Data = data;

        if (data.win)
        {
            if(winGo)
                winGo.gameObject.SetActive(true);
            if(loseGo)
                loseGo.gameObject.SetActive(false);
            
            if(starPref && starParent)
            {
                StartCoroutine(ShowStar(data.star));
            }
            
            //rewardController.gameObject.SetActive(true);
            //rewardController.Init(data.level,OnProgressAnimEnd);
        }
        else
        {
            //loseBtn.ExecuteClick();
            if(winGo)
                winGo.gameObject.SetActive(false);
            if(loseGo)
                loseGo.gameObject.SetActive(true);
          
            //InitRoleModel(data.role.roleId);
        }

        if(levelText)
            levelText.text = $"level{data.level}" ;
        
        SetAddScore( data.coin);

    }

    IEnumerator ShowStar(int star)
    {
        var list = new Star[3];
        for (int i = 0; i < 3; i++)
        {
            Star newItem = Instantiate(starPref, this.starParent);

            newItem.gameObject.SetActive(true);
            newItem.Init(false);

            list[i] = newItem;
        }
        for (int i = 0; i < list.Length; i++)
        {
            list[i].Init(i < star);

            yield return new WaitForSeconds(0.3f);
        }
    }

    /*
    void OnProgressAnimEnd()
    {
        uiListener?.OnProgressAnimEnd();

        DelayAction.DelayDo(0.5f,(() =>
        {
            adMultiBtn.SetActive(true);
            nextBtn.SetActive(true);
        }));
    }
    */

    public void SetAddScore(float coin)
    {
        if (addScoreText)
            addScoreText.text =   NumShow.GetNumShowStr(coin).ToString();
    }

    public GameObject loseItBtn;
    public void HideAdMultiBtn()
    {
        if(this.adMultiBtn)
            adMultiBtn.SetActive(false);
        nextBtn.SetActive(true);
        if(loseGo)
            loseItBtn.SetActive(false);
    }
    
    /*void InitRoleModel(int roleId)
    {
        ClearModel();
        
        var rolePref = GameResourceManager.LoadRolePerformancePrefab(roleId);

        var role = Instantiate(rolePref, this.modelParent);

        var trans = role.transform.GetComponentsInChildren<Transform>();
        trans.ForEach(e=>e.gameObject.layer = LayerMask.NameToLayer("UI"));

        Animator animator = role.GetComponentInChildren<Animator>();
        if (animator)
            animator.runtimeAnimatorController = this.loseAnimCtrl;
        
        role.transform.localPosition = Vector3.zero;
    }
    
    void ClearModel()
    {
        for (int i = 0; i < modelParent.childCount; i++)
        {
            DestroyImmediate(modelParent.GetChild(i).gameObject);
        }

    }*/
    

    public void OnClickAnotherGame()
    {
        uiListener?.OnClickAgain();
    }

    public void OnClickSkip()
    {
        uiListener?.OnClickSkip();
    }
    
    public void OnClickAdMulti()
    {
        uiListener?.OnClickAdMulti();
    }
    public void OnClicNext()
    {
        uiListener?.OnClickNext();
    }

    public void OnClickGoBack()
    {
        uiListener?.OnClickGoBack();
    }

    public void Release()
    {
        
    }
}
